home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / lh211src.zip / APPEND.C < prev    next >
C/C++ Source or Header  |  1991-02-19  |  4KB  |  212 lines

  1. /***********************************************************
  2.     append.c -- append to archive
  3. ***********************************************************/
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <farstr.h>
  8. #include <io.h>
  9. #include <dos.h>
  10. #include "lh.h"
  11. #include "intrface.h"
  12. #include "disp.h"
  13. #include "errmes.h"
  14.  
  15. struct interfacing interface;
  16.  
  17. void copylzh(void)
  18. {
  19.     update_arcstamp();
  20.     writehdr();
  21.     copyfile(file1, file2, hpb.packed, 0);
  22. }
  23.  
  24. struct extension {
  25.     struct extension *nxt;
  26.     char ext[1];
  27. };
  28.  
  29. struct extension *extptr;
  30.  
  31. void regext(char *ext)
  32. {
  33.     struct extension *p;
  34.  
  35.     if (*ext == '.') ext++;
  36.     p = malloc(strlen(ext) + sizeof(struct extension));
  37.     p -> nxt = extptr;
  38.     strcpy(p -> ext, ext);
  39.     extptr = p;
  40. }
  41.  
  42. static int isarc(char *name)
  43. {
  44.     static char arc[6][4] = {"zoo", "arc", "pak", "zip", "lzs", "lzh"};
  45.     char *p;
  46.     struct extension *q;
  47.     int i;
  48.  
  49.     p = strrchr(name, '.');
  50.     if (p == NULL) p = name + strlen(name) - 1;
  51.     p++;
  52.     for (i = 0; i < sizeof(arc) / 3; i++) {
  53.         if (*matchfname(arc[i], p) == '\0') return 1;
  54.     }
  55.     for (q = extptr; q; q = q -> nxt) {
  56.         if (*matchfname(q -> ext, p) == '\0') return 1;
  57.     }
  58.     return 0;
  59. }
  60.  
  61. void freeze(char *path, char *name)
  62. {
  63.     static int method = -1;
  64.  
  65.     hpb.pathname = name;
  66.     hpb.filename = getfilename(name);
  67.     file3 = myropen(path);
  68.  
  69.     if (method < 0) {
  70.         method = 5;
  71.         if (flg_o) method = 1;
  72.         if (flg_z == 1)    method = 0;
  73.         if (method > 0) method = encode_alloc(method);
  74.     }
  75.     _dos_getfileattr(path, &hpb.attr);
  76.     if (!flg_a) hpb.attr &= 0x21;
  77.     hpb.utc = getfiletime(file3);
  78.     hpb.level = flg_h;
  79.     hpb.info = 0;
  80.     hpb.original = hpb.packed = filelength(fileno(file3));
  81.     strcpy(hpb.method, "-lh5-");
  82.     interface.method = method;
  83.     interface.dicbit = (method == 1 || method == 4) ? 12 : 13;
  84.     interface.original = hpb.original;
  85.     regdisp("Freezing", path);
  86.     disp(0, outredir);
  87.     initdisp();
  88.  
  89.     if (hpb.original == 0 || flg_z == 2 && isarc(hpb.filename))
  90.         interface.method = 0;
  91.  
  92.     if (interface.method > 0) {
  93.         makehdr();
  94.         writehdr();
  95.         interface.infile = file3;
  96.         interface.outfile = file2;
  97.         encode(&interface);
  98.         if (interface.packed >= interface.original) {
  99.             rewind(file3);
  100.             fseek(file2, hpb.currentpos, SEEK_SET);
  101.             interface.method = 0;
  102.             dispname();
  103.             initdisp();
  104.         }
  105.         hpb.packed = interface.packed;
  106.     }
  107.     hpb.method[3] = interface.method + '0';
  108.     if (interface.method == 0) {
  109.         makehdr();
  110.         writehdr();
  111.         copyfile(file3, file2, hpb.original, 1);
  112.     }
  113.     update_arcstamp();
  114.     hpb.filecrc = crc;
  115.     adjusthdr();
  116.     fclose(file3);
  117.     if (ferror(file2)) error(WTERR, filename2);
  118.     if (flg_n != 2)
  119.         eprintf((flg_l) ? "\r  ==> %3d%%\n" : "\r==> %3d%%\n", 
  120.                 ratio(hpb.packed, hpb.original, 2));
  121. }
  122.  
  123. static void freeze_fb(struct fb far *fp)
  124. {
  125.     char *p;
  126.     char far *path;
  127.  
  128.     path = fp -> fname;
  129.     p = e_malloc(far_strlen(path) + 1);
  130.     far_strcpy(p, path);
  131.     freeze(p, p + (fp -> cpos - path));
  132.     free(p);
  133. }
  134.  
  135. int append(void)
  136. {
  137.     struct fb far *fp;
  138.  
  139.     if ((fp = searchfile(hpb.pathname)) != NULL
  140.         && (flg_c || fp -> time > hpb.utc))
  141.     {
  142.         freeze_fb(fp);
  143.         return 1;
  144.     } else {
  145.         if (fp != NULL) increment_disp();
  146.         copylzh();
  147.         return 0;
  148.     }
  149. }
  150.  
  151. int endappend(void)
  152. {
  153.     struct fb far *fp;
  154.     int modified;
  155.  
  156.     modified = 0;
  157.     for (fp = fbuf; fp != NULL; fp = fp -> nxt) {
  158.         if (fp -> used == 0){
  159.             freeze_fb(fp);
  160.             modified++;
  161.         }
  162.     }
  163.     return modified;
  164. }
  165.  
  166. void deletefiles(void)
  167. {
  168.     struct fb far *fp;
  169.  
  170.     for (fp = fbuf; fp != NULL; fp = fp -> nxt) {
  171.         far_strcpy(work, fp -> fname);
  172.         remove(work);
  173.     }
  174. }
  175.  
  176. int freshen(char far *bdir)
  177. {
  178.     char *path, *name;
  179.     struct find_t srchbuf;
  180.     int flag;
  181.  
  182.     if (bdir) {
  183.         path = e_malloc(far_strlen(bdir) + strlen(hpb.pathname) + 1);
  184.         far_strcpy(path, bdir);
  185.         if (*hpb.pathname == DELIM) {
  186.             if (path[1] == ':') {
  187.                 path[2] = '\0';
  188.             } else {
  189.                 *path = '\0';
  190.             }
  191.         }
  192.         name = path + strlen(path);
  193.         strcpy(name, hpb.pathname);
  194.         flag = !_dos_findfirst(path, flg_a ? 0x07 : 0x01, &srchbuf);
  195.         if (flag) {
  196.             if (!flg_c) {
  197.                 if (hpb.utc >= dos2unix((struct ftime *)&(srchbuf.wr_time)))
  198.                     flag = 0;
  199.             }
  200.         }
  201.     } else {
  202.         flag = 0;
  203.     }
  204.     if (flag) {
  205.         freeze(path, name);
  206.     } else {
  207.         copylzh();
  208.     }
  209.     free(path);
  210.     return flag;
  211. }
  212.